package edu.mapi.ir.viewer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.util.Hashtable;

import javax.swing.JPanel;

import edu.mapi.ir.gui.GuiUtils;
/**
 * @author ZP
 */
public class ScorePanel extends JPanel {

	private BufferedImage normalImg;
	private BufferedImage colidedImg;
	private String mouseName;
	private MousePlayer mouse;
	private Color mouseColor = Color.white;
	
	private static Hashtable<String, Image> ledImages = new Hashtable<String, Image>();
	
	static {
		ledImages.put("finish_off", GuiUtils.getImage("imgs/leds/finish_off.png"));
		ledImages.put("finish_on", GuiUtils.getImage("imgs/leds/finish_on.png"));
		ledImages.put("start_off", GuiUtils.getImage("imgs/leds/start_off.png"));
		ledImages.put("start_on", GuiUtils.getImage("imgs/leds/start_on.png"));
		ledImages.put("stop_off", GuiUtils.getImage("imgs/leds/stop_off.png"));
		ledImages.put("stop_on", GuiUtils.getImage("imgs/leds/stop_on.png"));
		ledImages.put("visit_off", GuiUtils.getImage("imgs/leds/visit_off.png"));
		ledImages.put("visit_on", GuiUtils.getImage("imgs/leds/visit_on.png"));
	}

	public ScorePanel(MousePlayer mousePlayer) {		
		setBackground(Color.black);
		this.mouse = mousePlayer;
		boolean wascolided = mousePlayer.isColision();
		mousePlayer.setColision(false);
		double[] sensorVals = mouse.distanceSensors;
		mouse.distanceSensors = new double[] {0,0,0,0};		
		normalImg = createMouseImage(mousePlayer, 50);
		mousePlayer.setColision(true);
		colidedImg = createMouseImage(mousePlayer, 50);		
		mousePlayer.setColision(wascolided);
		mouseName = mousePlayer.getPlayerName();
		mouseColor = mousePlayer.getColor();
		mouse.distanceSensors = sensorVals;
	}
	
	public void update(MousePlayer mouse) {
		this.mouse = mouse;
	}
	
	public void paint(Graphics g) {		
		super.paint(g);		
		
		Graphics2D g2 = (Graphics2D) g;
		
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setPaint(new GradientPaint(5,5,Color.black,5,getHeight()-10,mouseColor.darker().darker()));
		g2.fill(new RoundRectangle2D.Double(10, 10, getWidth() - 20, getHeight() - 20, 20, 20));
		g2.setColor(Color.white);
		g2.setStroke(new BasicStroke(2.0f));
		g2.draw(new RoundRectangle2D.Double(10, 10, getWidth() - 20, getHeight() - 20, 20, 20));
		BufferedImage img = mouse.isColision() ? colidedImg : normalImg;
		g.drawImage(img, 0,0,this);
		
		g.setColor(Color.white);
		g.setFont(new Font("Helvetica", Font.ITALIC, 20));
		g.drawString(mouseName, 60, 36);
						
		Image start = (mouse.getState().equalsIgnoreCase("running"))? ledImages.get("start_on") : ledImages.get("start_off");
		Image stop = (mouse.getState().equalsIgnoreCase("running"))? ledImages.get("stop_off") : ledImages.get("stop_on");
		Image visit = (mouse.getVisitedMask() != 0)? ledImages.get("visit_on") : ledImages.get("visit_off");
		Image finish = (mouse.getVisitedMask() != 0)? ledImages.get("finish_on") : ledImages.get("finish_off");

		g.drawImage(start, getWidth() - 60, 10, this);
		g.drawImage(stop, getWidth() - 60, 35, this);
		g.drawImage(visit, getWidth() - 60, 60, this);
		g.drawImage(finish, getWidth() - 60, 85, this);
		g.setColor(Color.yellow);
		g.setFont(new Font("Helvetica", Font.BOLD, 16));
		g.drawString("Time", 25, 75);
		g.drawString("Score", 80, 75);
		g.drawString("Hits", 140, 75);
		g.setColor(Color.white);
		g.setFont(new Font("Helvetica", Font.PLAIN, 14));
		g.drawString(""+mouse.getTime(), 30, 95);
		g.drawString(""+mouse.getScore(), 85, 95);
		g.drawString(""+mouse.getCollisions(), 145, 95);
	}
	
	private BufferedImage createMouseImage(MousePlayer mouse, int width) {
		
		BufferedImage img = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = img.createGraphics();
		
		g.translate(width/2,width/2);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		mouse.paintMouse(g, ((double)width/1.2));
		
		return img;
	}
	
	public static void main(String[] args) {
		MousePlayer mp = new MousePlayer(new Point2D.Double(12, 34.12));
		//mp = new MousePlayer(new Point2D.Double(12, 34.12));
		//mp = new MousePlayer(new Point2D.Double(12, 34.12));
		mp = new MousePlayer(new Point2D.Double(12, 34.12));
		mp.setDirection(30);
		//mp.setColision(true);
		GuiUtils.testFrame(new ScorePanel(mp));
	}
}
